// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// P [author] rhyme_lph
// t [email]  rhymelph@gmail.com
// R [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/app_spaces.dart';
import 'package:finance_app/src/utils/reg_exp_utils.dart';
import 'package:finance_app/src/widgets/list_item/list_header_item.dart';
import 'package:finance_app/src/widgets/common_app_bar.dart';
import 'package:finance_app/src/widgets/form/down_button_form.dart';
import 'package:finance_app/src/widgets/input/search_input_field.dart';
import 'package:finance_app/src/widgets/list_item/employee_item.dart';
import 'package:finance_app/src/widgets/select/employee_item_radio.dart';
import 'package:flutter/material.dart';
import 'package:finance_app/src/provider/hr_payroll/mark_as_resigned_provider.dart';
import 'package:finance_app/src/router_gen/router_meta.dart';
import 'package:flutter_sticky_header/flutter_sticky_header.dart';

import 'employee/new_employee_page.dart';

@RouterMeta(paramName: "markAsResignedPage")
class MarkAsResignedPage extends StatefulWidget {
  @override
  _MarkAsResignedPageState createState() => _MarkAsResignedPageState();
}

class _MarkAsResignedPageState extends State<MarkAsResignedPage>
    with PagePresenter<MarkAsResignedPage, MarkAsResignedProvider> {
  @override
  PreferredSizeWidget get appBar =>
      getDefaultAppBar(title: S.current.mark_as_resigned);

  @override
  Widget get buildWidget => ColoredBox(
        color: Colors.white,
        child: Column(
          children: [
            AppSpaces.h25,
            SearchInputField(
              controller: provider.searchController,
              focusNode: provider.searchFocusNode,
            ),
            AppSpaces.h20,
            Expanded(
              child: listBuilder<String>(
                selector: (_, __) => __.searchController.text,
                builder: (BuildContext context, String value, Widget child) =>
                    CustomScrollView(
                  slivers: buildSearchWidget(),
                ),
              ),
            ),
          ],
        ),
      );

  List<Widget> buildSearchWidget() {
    return provider.groupMap.entries
        .toList()
        .map((e) => buildSliver(e.key, e.value))
        .toList();
  }

  Widget buildSliver(String title, List<EmployeeContactEntity> data) =>
      SliverStickyHeader(
        header: buildSliverHeader(title: title),
        sliver: SliverList(
          delegate: SliverChildBuilderDelegate(
            (context, i) => buildSliverItem(data, i),
            childCount: data.length,
          ),
        ),
      );

  Widget buildSliverItem(List<EmployeeContactEntity> data, int i) {
    return listBuilder<EmployeeContactEntity>(
      selector: (_, __) => __.currentSelect,
      builder:
          (BuildContext context, EmployeeContactEntity value, Widget child) =>
              PressedOpacityButton(
        onPressed: () => provider.onSelectEmployee(data[i]),
        child: EmployeeItemRadio(
          title: data[i].name,
          content: data[i].title,
          isSelect: value == data[i],
          isLastItem: data.length == i + 1,
        ),
      ),
    );
  }

  Widget buildSliverHeader({String title}) {
    final String _title = title.isEmpty ? "*" : title;
    return ListHeaderItem(
      title: _title,
    );
  }

  bool _onSearchInputGroup(
      MapEntry<String, List<EmployeeContactEntity>> element) {
    return _onSearchInputItem(element.value).length > 0;
  }

  List<EmployeeContactEntity> _onSearchInputItem(
      List<EmployeeContactEntity> element) {
    String searchInput = provider.searchController.text;
    List<EmployeeContactEntity> data = [];
    for (final item in element) {
      if (RegExpUtils.getSearchRegExp(searchInput).hasMatch(item.name)) {
        data.add(item);
      }
    }
    return data;
  }
}
